#include "mytcpsockettoall.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>

MyTcpSocketToAll::MyTcpSocketToAll(QObject *parent)
    : MyTcpSocket{parent}
{

}

void MyTcpSocketToAll::sendDataToIdentitySlot(int identity, QByteArray data)
{
    if(identity != this->identity)
        return;
    this->write(data);

}

void MyTcpSocketToAll::handleFrame(const QJsonObject &obj)
{
    //区分数据类型
    int type = obj.value("type").toInt();
    if(type == 5000)
        handleIdentityFrame(obj);
    else if (type == 3001) {
        QJsonObject reObj = obj;
        reObj.insert("type",1001);
        emit sendDataToIdentity(0,QJsonDocument(reObj).toJson());
    }
    else if (type >=2001&& type<= 2004) {
        QJsonObject reObj = obj;
        reObj.insert("type",type + 2000);
        emit sendDataToIdentity(2,QJsonDocument(reObj).toJson());
    }
    else if (type == 5001) {
        QJsonObject reObj = obj;
        reObj.insert("type",1002);
        emit sendDataToIdentity(0,QJsonDocument(reObj).toJson());
        reObj.insert("type",4005);
        emit sendDataToIdentity(2,QJsonDocument(reObj).toJson());

    }
    else if (type == 2005) {
        QJsonObject reObj = obj;
        reObj.insert("type",6001);
        emit sendDataToIdentity(1,QJsonDocument(reObj).toJson());

    }
    else if (type == 3002) {
        QJsonObject reObj = obj;
        reObj.insert("type",1003);
        emit sendDataToIdentity(0,QJsonDocument(reObj).toJson());
    }
}

void MyTcpSocketToAll::handleIdentityFrame(const QJsonObject &obj)
{
   int identity = obj.value("identity").toInt();
   this->identity = identity;
}
